<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>json_decode和json_encode的使用方法 | 尹义斌•Blog | MyBlog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="PHP">
    <meta name="description" content="json_decode对json格式的字符串进行编码也就是转化成PHP变量123$content = $this-&amp;gt;curl-&amp;gt;get(&amp;quot;http://sj.apidata.cn/?mobile=&amp;#123;$phone&amp;#125;&amp;quot;);$contents = json_decode($content);print_r(contents); 打印结果(结果是一个对象">
<meta name="keywords" content="PHP">
<meta property="og:type" content="article">
<meta property="og:title" content="json_decode和json_encode的使用方法">
<meta property="og:url" content="http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/index.html">
<meta property="og:site_name" content="尹义斌•Blog">
<meta property="og:description" content="json_decode对json格式的字符串进行编码也就是转化成PHP变量123$content = $this-&amp;gt;curl-&amp;gt;get(&amp;quot;http://sj.apidata.cn/?mobile=&amp;#123;$phone&amp;#125;&amp;quot;);$contents = json_decode($content);print_r(contents); 打印结果(结果是一个对象">
<meta property="og:updated_time" content="2017-07-20T03:27:11.064Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="json_decode和json_encode的使用方法">
<meta name="twitter:description" content="json_decode对json格式的字符串进行编码也就是转化成PHP变量123$content = $this-&amp;gt;curl-&amp;gt;get(&amp;quot;http://sj.apidata.cn/?mobile=&amp;#123;$phone&amp;#125;&amp;quot;);$contents = json_decode($content);print_r(contents); 打印结果(结果是一个对象">
    
        <link rel="alternate" type="application/atom+xml" title="尹义斌•Blog" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">尹义斌</h5>
          <a href="mailto:yinyibin5683@163.com" title="yinyibin5683@163.com" class="mail">yinyibin5683@163.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                文章
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/yyb568" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://weibo.com/yyb5683" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                微博
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/custom"  >
                <i class="icon icon-lg icon-link"></i>
                测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">json_decode和json_encode的使用方法</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">json_decode和json_encode的使用方法</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-07-20T00:44:43.260Z" itemprop="datePublished" class="page-time">
  2017-07-20
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#json-decode对json格式的字符串进行编码也就是转化成PHP变量"><span class="post-toc-number">1.</span> <span class="post-toc-text">json_decode对json格式的字符串进行编码也就是转化成PHP变量</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#json-decode-对PHP变量进行-JSON-编码"><span class="post-toc-number">2.</span> <span class="post-toc-text">json_decode 对PHP变量进行 JSON 编码</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#最后"><span class="post-toc-number">3.</span> <span class="post-toc-text">最后:</span></a></li></ol>
        </nav>
    </aside>
    
<article id="post-json_decode和json_encode的使用方法"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">json_decode和json_encode的使用方法</h1>
        <div class="post-meta">
            <time class="post-time" title="2017-07-20 08:44:43" datetime="2017-07-20T00:44:43.260Z"  itemprop="datePublished">2017-07-20</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="json-decode对json格式的字符串进行编码也就是转化成PHP变量"><a href="#json-decode对json格式的字符串进行编码也就是转化成PHP变量" class="headerlink" title="json_decode对json格式的字符串进行编码也就是转化成PHP变量"></a>json_decode对json格式的字符串进行编码也就是转化成PHP变量</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$content = $this-&gt;curl-&gt;get(&quot;http://sj.apidata.cn/?mobile=&#123;$phone&#125;&quot;);</div><div class="line">$contents = json_decode($content);</div><div class="line">print_r(contents);</div></pre></td></tr></table></figure>
<p>打印结果(结果是一个对象,如果要取出里面的数值,这样是不好取的):<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">stdClass Object</div><div class="line">(</div><div class="line">    [status] =&gt; 1</div><div class="line">    [data] =&gt; stdClass Object</div><div class="line">        (</div><div class="line">            [prefix] =&gt; 131</div><div class="line">            [province] =&gt; 山东</div><div class="line">            [city] =&gt; 威海</div><div class="line">            [isp] =&gt; 联通</div><div class="line">            [code] =&gt; 631</div><div class="line">            [zipcode] =&gt; 264200</div><div class="line">            [types] =&gt; 中国联通 GSM</div><div class="line">            [mobile] =&gt; 13181891701</div><div class="line">        )</div><div class="line"></div><div class="line">    [message] =&gt; success</div><div class="line">)</div></pre></td></tr></table></figure></p>
<font color="#2FA3F6">当json_decode($contents,true)时会直接打印出PHP数组.</font>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$content = $this-&gt;curl-&gt;get(&quot;http://sj.apidata.cn/?mobile=&#123;$phone&#125;&quot;);</div><div class="line">$contents = json_decode($content,true);</div><div class="line">print_r($contents);</div></pre></td></tr></table></figure>
<font color="#2FA3F6">打印结果 PHP数组:</font>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">Array</div><div class="line">(</div><div class="line">    [status] =&gt; 1</div><div class="line">    [data] =&gt; Array</div><div class="line">        (</div><div class="line">            [prefix] =&gt; 131</div><div class="line">            [province] =&gt; 山东</div><div class="line">            [city] =&gt; 威海</div><div class="line">            [isp] =&gt; 联通</div><div class="line">            [code] =&gt; 631</div><div class="line">            [zipcode] =&gt; 264200</div><div class="line">            [types] =&gt; 中国联通 GSM</div><div class="line">            [mobile] =&gt; 13181891701</div><div class="line">        )</div><div class="line"></div><div class="line">    [message] =&gt; success</div><div class="line">)</div></pre></td></tr></table></figure>
<font color="#FF4081">可以看出 json_decode($contents,true)输出的一个关联数组,由此可知json_decode($contents）输出的是对象,而json_decode(“$contents”,true)是把它强制生成PHP关联数组</font>

<h2 id="json-decode-对PHP变量进行-JSON-编码"><a href="#json-decode-对PHP变量进行-JSON-编码" class="headerlink" title="json_decode 对PHP变量进行 JSON 编码"></a>json_decode 对PHP变量进行 JSON 编码</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">$arr = array (&apos;a&apos;=&gt;1,&apos;b&apos;=&gt;2,&apos;c&apos;=&gt;3,&apos;d&apos;=&gt;4,&apos;e&apos;=&gt;5); </div><div class="line"></div><div class="line">echo json_encode($arr); </div><div class="line">?&gt;</div></pre></td></tr></table></figure>
<p><font color="#2FA3F6">打印结果 PHP数组:</font><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123;&quot;a&quot;:1,&quot;b&quot;:2,&quot;c&quot;:3,&quot;d&quot;:4,&quot;e&quot;:5&#125;</div></pre></td></tr></table></figure></p>
<h2 id="最后"><a href="#最后" class="headerlink" title="最后:"></a>最后:</h2><p>可以看出json_encode()和json_decode()是编译和反编译过程，注意json只接受utf-8编码的字符，所以json_encode()的参数必须是utf-8编码，否则会得到空字符或者null。</p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-07-20T03:27:11.064Z" itemprop="dateUpdated">2017-07-20 11:27:11</time>
</span><br>


        
    </div>
    <footer>
        <a href="http://yybblog.cn">
            <img src="/img/avatar.jpg" alt="尹义斌">
            尹义斌
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/PHP/">PHP</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/&title=《json_decode和json_encode的使用方法》 — 尹义斌•Blog&pic=http://yybblog.cn/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/&title=《json_decode和json_encode的使用方法》 — 尹义斌•Blog&source=一个技术博客和生活琐碎博客" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《json_decode和json_encode的使用方法》 — 尹义斌•Blog&url=http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/&via=http://yybblog.cn" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/07/20/stripos的用法/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">stripos()的用法</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/07/18/如何在七牛云储存创建及使用空间/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">如何在七牛云储存创建及使用空间</h4>
      </a>
    </div>
  
</nav>



    














</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/&title=《json_decode和json_encode的使用方法》 — 尹义斌•Blog&pic=http://yybblog.cn/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/&title=《json_decode和json_encode的使用方法》 — 尹义斌•Blog&source=一个技术博客和生活琐碎博客" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《json_decode和json_encode的使用方法》 — 尹义斌•Blog&url=http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/&via=http://yybblog.cn" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yybblog.cn/2017/07/20/json_decode和json_encode的使用方法/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
